/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import clases.*;
import java.awt.BorderLayout;

/**
 *
 * @author sergio crisologo
 */
public class dlgUsuariosRoles extends javax.swing.JDialog {

    private ClsDatabase bd = new ClsDatabase();
    private ClsSeguridad seguridad = new ClsSeguridad();
    private ClsSesion sesion = new ClsSesion();
    private ClsMetodos metodos = new ClsMetodos();
    /**
     * Creates new form dlgUsuarios
     *
     * @param parent
     * @param modal
     */
    Object[][] array_id_usuario_tipo;
    Object[][] array_formulario_faltantes;
    Object[][] array_formulario_usados;

    public dlgUsuariosRoles(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        //metodos.tablaLlenaSimple(jTable1, bd.select("usuario_tipo", "descripcion,descripcion", null));
        //dlgInsumos in = new dlgInsumos(new javax.swing.JFrame(), true);
        //jPanel1=in.panelFondo1;
        //jPanel1.add(in.panelFondo1);
        //jPanel1.add(in.panelFondo1, BorderLayout.CENTER);
        //this.repaint();
        //jPanel1.getContentPane().add(new usuarios());
        //System.out.println("haver:  "+this.);
        array_id_usuario_tipo = bd.select("usuario_tipo", "idusuario_tipo", null);
        metodos.tablaLlenaSql_antes(jTable1, "SELECT descripcion,estado FROM usuario_tipo", 2);
        setLocationRelativeTo(null);
    }

    public void usuario_formulario() {
        int fila_seleccionada = jTable1.getSelectedRow();
        if (fila_seleccionada != -1) {
            //System.out.println( array_id_usuario_tipo[fila_seleccionada][0]);
            String descripcion_tipo_usuario = jTable1.getValueAt(fila_seleccionada, 0).toString();
            jTextField1.setText(descripcion_tipo_usuario);
            /*lista de formularios faltantes*/
            cargar_formularios_fatantes(fila_seleccionada);
            cargar_formularios_usadados(fila_seleccionada);

//            metodos.tablaLlenaSql(jTable2, "SELECT nombre FROM formulario", 1);

        }
    }

    public void cargar_formularios_fatantes(int fila) {
        metodos.tablaLlenaSql_antes(jTable2, "SELECT nombre FROM formulario where not idformulario in (select idformulario from usuario_formulario where idusuario_tipo='" + array_id_usuario_tipo[fila][0] + "')", 1);
    }

    public void cargar_formularios_usadados(int fila) {
        metodos.tablaLlenaSql_antes(jTable3, "SELECT f.nombre,uf.permiso FROM usuario_formulario uf, formulario f where uf.idformulario=f.idformulario and uf.idusuario_tipo='" + array_id_usuario_tipo[fila][0] + "'", 2);
    }

    public void agregar() {
        int fila_1 = jTable1.getSelectedRow();
        if (fila_1 != -1) {
            int fila_2 = jTable2.getSelectedRow();
            if (fila_2 != -1) {
                array_formulario_faltantes = bd.select("formulario", "idformulario", " not idformulario in (select idformulario from usuario_formulario where idusuario_tipo='" + array_id_usuario_tipo[fila_1][0] + "')");
                bd.insert("usuario_formulario", "idusuario_tipo,idformulario,permiso", "'" + array_id_usuario_tipo[fila_1][0] + "','" + array_formulario_faltantes[fila_2][0] + "','"+ jComboBox1.getSelectedItem().toString().substring(0,1) +"'");
                cargar_formularios_fatantes(fila_1);
                cargar_formularios_usadados(fila_1);
            }

        }
    }

    public void quitar() {
        int fila_1 = jTable1.getSelectedRow();
        if (fila_1 != -1) {
            int fila_2 = jTable3.getSelectedRow();
            if (fila_2 != -1) {
                
                array_formulario_usados = bd.select("usuario_formulario uf, formulario f ", "f.idformulario", " uf.idformulario=f.idformulario and uf.idusuario_tipo='" + array_id_usuario_tipo[fila_1][0] + "'");
                bd.delete("usuario_formulario", "idusuario_tipo='" + array_id_usuario_tipo[fila_1][0] + "' and idformulario='" + array_formulario_usados[fila_2][0] + "'");
//
                cargar_formularios_fatantes(fila_1);
                cargar_formularios_usadados(fila_1);
            }

        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        shapeTabbedPaneUI1 = new util.shapeTabbedPaneUI();
        shapeTabbedPaneUI2 = new util.shapeTabbedPaneUI();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Roles");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(225, 235, 247));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Tipo de usuario", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_NEXT_COLUMN);
        jTable1.setOpaque(false);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.setShowHorizontalLines(false);
        jTable1.setShowVerticalLines(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jTable1MouseEntered(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable1MousePressed(evt);
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTable1KeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 43, 617, 109));

        jLabel1.setText("Roles:");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        jLabel2.setText("Tipo de usuario:");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 177, -1, -1));
        jPanel1.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(108, 174, 126, -1));

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Formulario"
            }
        ));
        jTable2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jTable2);

        jPanel1.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 206, 224, 239));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Permitido", "Ver" }));
        jPanel1.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(281, 226, 71, -1));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Permiso");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(281, 206, 71, -1));

        jButton2.setText("Agregar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(281, 257, -1, -1));

        jButton1.setText("Quitar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(281, 298, 71, -1));

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Formulario permitidos", "Permiso"
            }
        ));
        jTable3.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(jTable3);

        jPanel1.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(379, 206, 248, 239));
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 158, 461, 10));

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/fondo_dialog.jpg"))); // NOI18N
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 450, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 670, 540));

        setBounds(0, 0, 679, 571);
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseEntered
    }//GEN-LAST:event_jTable1MouseEntered

    private void jTable1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyReleased
        usuario_formulario();
    }//GEN-LAST:event_jTable1KeyReleased

    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
        usuario_formulario();
    }//GEN-LAST:event_jTable1MousePressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        agregar();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        quitar();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlgUsuariosRoles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlgUsuariosRoles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlgUsuariosRoles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgUsuariosRoles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgUsuariosRoles dialog = new dlgUsuariosRoles(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextField jTextField1;
    private util.shapeTabbedPaneUI shapeTabbedPaneUI1;
    private util.shapeTabbedPaneUI shapeTabbedPaneUI2;
    // End of variables declaration//GEN-END:variables
}
